/**
 * 
 */
package dms.presentation.action.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import dms.dto.StudentAffairTeacherDTO;
import dms.model.School;
import dms.model.role.StudentAffairTeacher;
import dms.model.role.wrapper.SystemAdministratorWrapper;
import dms.model.util.PageIterator;
import dms.service.SchoolService;
import dms.service.SystemAdministratorQuerySerivce;
import dms.util.Constant;
import dms.util.ContextUtil;

/**
 * @author 李健
 *
 */
public class TeacherAction extends DispatchAction {
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		SystemAdministratorQuerySerivce systemAdministratorQuerySerivce = (SystemAdministratorQuerySerivce) ContextUtil.getContext().getBean("systemAdministratorQuerySerivce");
		int start;
		int count;
		try {
			start = Integer.valueOf(request.getParameter("start"));
			count = Integer.valueOf(request.getParameter("count"));
		} catch (Exception e) {
			start = Constant.START;
			count = Constant.COUNT;
		} finally {
			
		}
				
		PageIterator<StudentAffairTeacher> iter = systemAdministratorQuerySerivce.findAllStudentAffairTeahcer(start, count);
		request.setAttribute("page", iter);
		
		return mapping.findForward("list");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		PageIterator<School> iter = schoolService.findAllSchool(0, 100);
		
		request.setAttribute("page", iter);
		return mapping.findForward("add");
	}	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		long id = Long.valueOf(request.getParameter("id"));
		
		SystemAdministratorWrapper saWrapper = (SystemAdministratorWrapper) ContextUtil.getContext().getBean("systemAdministratorWrapper");
		try {
			saWrapper.deleteStudentAffairTeacherById(id);
		} catch (Exception e) {			
			e.printStackTrace();
			return mapping.findForward(Constant.FAILURE);
		}
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		StudentAffairTeacherDTO dto = (StudentAffairTeacherDTO) form;
		
		SystemAdministratorWrapper wrapper = (SystemAdministratorWrapper) ContextUtil.getContext().getBean("systemAdministratorWrapper");
		
		try {
			wrapper.updateStudentAffairTeacher(dto);
		} catch (Exception e) {			
			e.printStackTrace();
			return mapping.findForward(Constant.FAILURE);
		}
		return mapping.findForward("list");
	}
	
	public ActionForward find(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		SystemAdministratorQuerySerivce systemAdministratorQuerySerivce = (SystemAdministratorQuerySerivce) ContextUtil.getContext().getBean("systemAdministratorQuerySerivce");
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		PageIterator<School> iter = schoolService.findAllSchool(0, 100);
		
		request.setAttribute("page", iter);
		long id;
		try {
			id = Long.valueOf(request.getParameter("id"));
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return mapping.findForward(Constant.FAILURE);
		}
		
		StudentAffairTeacher sat = systemAdministratorQuerySerivce.findStudentAffairTeacherById(id);
		request.setAttribute("teacher", sat);
		return mapping.findForward("update");
	}
	
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		return this.list(mapping, form, request, response);
	}
}






